package br.inf.ufrgs.usixml4desktop.rendering.javadesktop.components;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JComponent;

import org.jdom.Element;

import br.inf.ufrgs.usixml4desktop.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4desktop.exceptions.RenderingErrorException;
import br.inf.ufrgs.usixml4desktop.rendering.UsiXMLLayoutManagerRenderer;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.RendererFactory;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;
import br.inf.ufrgs.usixml4desktop.rendering.javadesktop.utils.InformationParser;

public class UsiXMLJavaDesktopGridBagBoxRenderer extends UsiXMLLayoutManagerRenderer {

	public UsiXMLJavaDesktopGridBagBoxRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
	}

	@Override
	public UsiXMLLayoutManagerRenderer getNextRenderer() {
		return new UsiXMLJavaDesktopFlowBoxRenderer((UsiXMLJavaDesktopInterfaceRenderer) this.usiXMLInterfaceRenderer);
	}

	@Override
	public String getValidElementName() {
		return "gridBagBox";
	}

	@Override
	public Container processRenderization(Element element, Container contentPane) 
		throws ParsingErrorException,RenderingErrorException {
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		
		contentPane.setLayout(gridbag);
		
		int gridx = 0;
		int gridy = 0;
		int gridwidth = 0;
        int gridheight = 0; 
        double weightx = 0; 
        double weighty = 0;
        int fill = 0;
        Insets insets = null;
		
                
		for (Object constraint : element.getChildren()) {
			Element child = (Element) constraint;
			
			try {
				gridx = NumberFormat.getIntegerInstance().parse(child.getAttributeValue("gridx")).intValue();
				gridy = NumberFormat.getIntegerInstance().parse(child.getAttributeValue("gridy")).intValue();
				gridwidth = NumberFormat.getIntegerInstance().parse(child.getAttributeValue("gridwidth")).intValue();
				gridheight = NumberFormat.getIntegerInstance().parse(child.getAttributeValue("gridheight")).intValue();
				weightx = NumberFormat.getNumberInstance().parse(child.getAttributeValue("weightx")).doubleValue();
				weighty = NumberFormat.getNumberInstance().parse(child.getAttributeValue("weighty")).doubleValue();
				
				InformationParser parser = new InformationParser();
				insets = parser.parseInsets(child.getAttributeValue("insets"));
								
				String fillString = child.getAttributeValue("fill");
				
				if (fillString.equals("both")){
					fill = GridBagConstraints.BOTH;
				} else if (fillString.equals("horizontal")){
					fill = GridBagConstraints.HORIZONTAL;
				} else if (fillString.equals("vertical")){
					fill = GridBagConstraints.VERTICAL;
				} else{
					throw new RenderingErrorException("Unknown fill type");
				}
				
				constraints.gridx = gridx;
				constraints.gridy = gridy;
				constraints.weightx = weightx;
				constraints.weighty = weighty;
				constraints.gridwidth = gridwidth;
				constraints.gridheight = gridheight;
				constraints.fill = fill;
				constraints.insets = insets;
				
				Element constraintChild = (Element) child.getChildren().toArray()[0];
				JComponent component = (JComponent) 
					RendererFactory.getInstance().
						getFirstElementRenderer((UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer).renderElement(constraintChild);
				
				contentPane.add(component,constraints);
				
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
		
		return contentPane;
		
	}
	
	@Override
	public boolean validateElement(Element element) {
		return true;
	}
}
